from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from django.views.decorators.http import require_http_methods
# 延迟导入避免循环引用
from exam_system.decorators import examinee_required

@examinee_required
def notification_list(request):
    """获取考生的通知列表"""
    from exam_system.models import ExamNotification

    notifications = ExamNotification.objects.filter(
        examinee=request.examinee,
        is_read=False
    ).order_by('-created_at')

    return render(request, 'exam_system/notifications.html', {
        'notifications': notifications
    })

@examinee_required
@require_http_methods(["POST"])
def mark_notification_as_read(request, notification_id):
    """将通知标记为已读"""
    from exam_system.models import ExamNotification

    try:
        notification = ExamNotification.objects.get(
            id=notification_id,
            examinee=request.user
        )
        notification.is_read = True
        notification.save()
        return JsonResponse({'status': 'success'})
    except ExamNotification.DoesNotExist:
        return JsonResponse({'status': 'error', 'message': '通知不存在'}, status=404)